package com.xu.controller.center;

import com.xu.controller.BaseController;
import com.xu.enums.YesOrNo;
import com.xu.pojo.OrderItems;
import com.xu.pojo.Orders;
import com.xu.pojo.bo.center.OrderItemsCommentBO;
import com.xu.service.center.MyCommentsService;
import com.xu.service.center.MyOrderService;
import com.xu.utils.JSONResult;
import com.xu.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.annotation.Order;

import java.util.List;

/**
 * @author anan
 * @create 2022-02-24 9:41
 */
@RestController
@RequestMapping("mycomments")
@Api(value = "用户中心评价模块", tags = "用户中心评价模块相关接口")
public class MyCommentController extends BaseController {

    @Autowired
    private MyCommentsService myCommentsService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/pending")
    public JSONResult pending(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId
            ) {

        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }
        Orders myOrder = (Orders)checkResult.getData();
        if(myOrder.getIsComment() == YesOrNo.Yes.type){
            return JSONResult.errorMsg("该笔订单已经评价");
        }
        List<OrderItems> list = myCommentsService.queryPendingComment(orderId);
        return JSONResult.ok(list);
    }

    @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST")
    @PostMapping("/saveList")
    public JSONResult saveList(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList) {
        System.out.println(commentList);
        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }
        //判断评论内容list不能为空
        if(commentList == null || commentList.isEmpty() || commentList.size() == 0){
            return JSONResult.errorMsg("评论内容不能为空！");
        }
        myCommentsService.saveComments(orderId, userId, commentList);
        List<OrderItems> list = myCommentsService.queryPendingComment(orderId);
        return JSONResult.ok();


    }

    @ApiOperation(value = "查询我的评价", notes = "查询我的评价", httpMethod = "POST")
    @PostMapping("/query")
    public JSONResult query(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "查询分页每一页的显示条数", required = false)
            @RequestParam Integer pageSize) {

        if(StringUtils.isBlank(userId)) return JSONResult.errorMsg(null);
        if(page == null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = COMMEN_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = myCommentsService.queryMyComments(userId, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }








}
